package test;

import java.io.File;

public class MultiThreadCopy{
        public static void main(String[] args) {
            File sourceFile = new File("E:\\javaIO\\myeclipse.exe");//源文件地址
            File targetDir = new File("e:/javaFile");//目标文件地址
            int threadNumber = 4;
            copyFile(sourceFile, targetDir, threadNumber);
        }

        public static void copyFile(File sourceFile, File targetDir, int threadNumber) {
            // TODO Auto-generated method stub
            if(!targetDir.exists()){
                targetDir.mkdirs();
            }
            File targetFile = new File(targetDir, sourceFile.getName());
            long length = sourceFile.length();
            long unitLength = length/threadNumber;
            for(int i=0; i<threadNumber; i++){
                long begin = 0;
                long end = length;
                if(i!=0){
                    begin = unitLength*i;
                }
                if(i!=threadNumber-1){
                    end = unitLength*(i+1)-1;
                }
                String name = "线程"+i;
                new MyThread(name, sourceFile, targetFile, begin, end).start();
                System.out.println(name+"启动完成!");
            }
        }
    }
